<include file="Public:header" />
<style>
    a.disabled{background: none;width:auto;height:auto;}
</style>
<!--文章列表-->
<div class="pad_lr_10" >
    <div class="col_tab">
    <ul class="J_tabs tab_but cu_li">
        <li><a id="export" href="javascript:;" autocomplete="off" class="disabled">立即备份</a></li>
        <li><a id="optimize" href="{:U('optimize')}">优化表</a></li>
        <li><a id="repair" href="{:U('repair')}">修复表</a></li>
        <li id="top-alert" class="hidden">
            <div class="alert-content" style="color:#ff0000;">这是内容</div>

        </li>
    </ul>
    <div class="J_tablelist table_list" data-acturi="">
        <form id="export-form" method="post" action="{:U('export')}">
            <table width="100%">
                <thead>
                <tr>
                    <th width="48" align="left"><input class="check-all" checked="chedked" type="checkbox" value=""></th>
                    <th align="left">表名</th>
                    <th width="120" align="left">数据量</th>
                    <th width="120" align="left">数据大小</th>
                    <th width="160" align="left">创建时间</th>
                    <th width="160" align="left">备份状态</th>
                    <th width="120" align="left">操作</th>
                </tr>
                </thead>
                <tbody>
                <volist name="list" id="table">
                    <tr>
                        <td class="num">
                            <input class="ids" checked="chedked" type="checkbox" name="tables[]" value="{$table.name}">
                        </td>
                        <td>{$table.name}</td>
                        <td>{$table.rows}</td>
                        <td>{$table.data_length|format_bytes}</td>
                        <td>{$table.create_time}</td>
                        <td class="info">未备份</td>
                        <td class="action">
                            <a class="ajax-get no-refresh" href="{:U('optimize?tables='.$table['name'])}">优化表</a>&nbsp;
                            <a class="ajax-get no-refresh" href="{:U('repair?tables='.$table['name'])}">修复表</a>
                        </td>
                    </tr>
                </volist>
                </tbody>
            </table>
        </form>

        </div>
    </div>
</div>
<include file="Public:footer" />
<script type="text/javascript">
    (function($){
        var $form = $("#export-form"), $export = $("#export"), tables
        $optimize = $("#optimize"), $repair = $("#repair");

        $optimize.add($repair).click(function(){
            $.post(this.href, $form.serialize(), function(data){
                if(data.status){
                    updateAlert(data.info,'alert-success');
                } else {
                    updateAlert(data.info,'alert-error');
                }

            }, "json");
            return false;
        });

        $export.click(function(){
            $export.parent().children().addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post(
                    $form.attr("action"),
                    $form.serialize(),
                    function(data){
                        if(data.status){
                            tables = data.tables;
                            $export.html(data.info + "开始备份，请不要关闭本页面！");
                            backup(data.tab);
                            window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                        } else {
                            updateAlert(data.info,'alert-error');
                            $export.parent().children().removeClass("disabled");
                            $export.html("立即备份");

                        }
                    },
                    "json"
            );
            return false;
        });

        function backup(tab, status){
            status && showmsg(tab.id, "开始备份...(0%)");
            $.get($form.attr("action"), tab, function(data){
                if(data.status){
                    showmsg(tab.id, data.info);

                    if(!$.isPlainObject(data.tab)){
                        $export.parent().children().removeClass("disabled");
                        $export.html("备份完成，点击重新备份");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(data.tab, tab.id != data.tab.id);
                } else {
                    updateAlert(data.info,'alert-error');
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");

                }
            }, "json");

        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
        //全选的实现
        $(".check-all").click(function(){
            $(".ids").prop("checked", this.checked);
        });
        $(".ids").click(function(){
            var option = $(".ids");
            option.each(function(i){
                if(!this.checked){
                    $(".check-all").prop("checked", false);
                    return false;
                }else{
                    $(".check-all").prop("checked", true);
                }
            });
        });
    })(jQuery);
</script>
</body>
</html>
